const typeLists = 'Undefined Null Boolean Number String Symbol Function Array Date RegExp Object Error Set WeakSet Map WeakMap Math JSON Arguments Window Buffer ArrayBuffer'.split(' ')

const class2type: {
  [key: string]: string
} = {}

typeLists.forEach((item) => {
  class2type[`[object ${item}]`] = item.toLowerCase()
})

/**
 * @description 获取变量类型
 * @param value {unknown} 值
 * @returns {string } 值所属类型(小写)
 * @example
 * ----------------------------
 * getClassType('123')
 * // => 'string'
 * ----------------------------
 * getClassType(123)
 * // => 'number'
 * ----------------------------
 * getClassType(null)
 * // => 'null'
 * ----------------------------
 * getClassType(undefined)
 * // => 'undefined'
 */
export const getType = (value: unknown): string => {
  if (value == null) return `${value}`
  if (value instanceof Element) return 'element'
  return typeof value === 'object' || typeof value === 'function'
    ? class2type[Object.prototype.toString.call(value)] || 'object'
    : typeof value
}
